import { observable, action, toJS } from 'mobx';
import { Tools, Toast } from 'weaver-mobile';
import { Browser, ListViewPage } from 'weaver-mobile-page';
// import { callApi } from '../../util/callapi';

export default class EntranceBasicStore {
  // @observable currentTab = 0;
  @observable accountLogin = false;
  @observable loading = true;
  @observable hasError = false;
  @observable errorMsg = '';
  @observable account = '';
  @observable password = '';
  @observable idNumber = '';
  @observable deviceId = '';
  @observable browser = new Browser.Store({ route: '/yulin-login' });
  @observable browserValue = [];

  @observable list = new ListViewPage.Store({ route: '/yulin-entrance' });
  @observable listParams = {};

  @observable selectedTab = 'Message';

  @action
  setState = (params = {}) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
  }

  @action
  doLogin = () => {
    function init(accountLogin, account, password, idNumber, deviceId) {
      let params = JSON.stringify({
        type: accountLogin ? '1' : '2',
        username: account,
        password: password,
        idcard: idNumber,
        devid: deviceId
      })

      Tools.callApi({
        url: '/api/integration/simplesso/getToken',
        method: 'POST',
        params: {
          loginInfo: params
        },
      }).then((res) => {
        if (res.status == true) {
          if (!res.ssoToken) {
            Toast.info('登录失败');
            return;
          }
          Tools.ls.set('customSessionKey', {
            keyName: 'ssoToken',
            keyValue: res.ssoToken
          })
          Toast.hide();
          Toast.info('登录成功', 2, () => {
            window.location.href = '/spa/entrance/index-lzlc.html'
          });
        } else {
          Toast.info('登录失败');
        }
      }).catch((res) => {
        Toast.error(JSON.stringify(res));
      })

    }

    let params = Tools.getFrameParams();
    let storageRes = Tools.ls.getJSONObj('yulinAuthDatas');
    if (params['isChange'] == '1' && storageRes !== null && storageRes) {
      this.idNumber = storageRes.staffIdno;
      this.deviceId = storageRes.imei;
      window.yulinRedirectParams = {
        "uid": this.idNumber,
        "staffCode": this.idNumber,
        "devId": this.deviceId,
        "devAddress": null,
        "appCode": "10000243",
        "reqServerId": "A2-BE339FCB9D83418592554A6B7F47A8BE",
        "targetServerId": "A3-E616FF2887F54CE4B3B8ABA47598362D",
      }
      init(toJS(this.accountLogin), toJS(this.account), toJS(this.password), toJS(this.idNumber), toJS(this.deviceId));
    } else {
      try {
        window.WebViewJavascriptBridge.callHandler(
          'getThirdAuthInfo'
          , {}
          , function (responseData) {
            var res = {};
            if (typeof (responseData) === 'string') {
              res = JSON.parse(responseData).data;
            } else {
              res = responseData.data
            }
            Tools.ls.set('yulinAuthDatas', res)
            window.yulinRedirectParams = {
              "uid": res.staffIdno,
              "staffCode": res.staffIdno,
              "devId": res.imei,
              "devAddress": null,
              "appCode": "10000243",
              "reqServerId": "A2-BE339FCB9D83418592554A6B7F47A8BE",
              "targetServerId": "A3-E616FF2887F54CE4B3B8ABA47598362D",
            }
            init(false, '', '', res.staffIdno, res.imei);
          }.bind(this)
        );
      } catch (error) {
        Toast.hide();
        Toast.info('获取设备信息失败，使用默认信息');
        this.idNumber = '450203198002220016';
        this.deviceId = '867011036792968';
        window.yulinRedirectParams = {
          "uid": this.idNumber,
          "staffCode": this.idNumber,
          "devId": this.deviceId,
          "devAddress": null,
          "appCode": "10000243",
          "reqServerId": "A2-BE339FCB9D83418592554A6B7F47A8BE",
          "targetServerId": "A3-E616FF2887F54CE4B3B8ABA47598362D",
        }
        init(false, '', '', toJS(this.idNumber), toJS(this.deviceId));
      }
    }
  }

  @action
  getMessageData = ( params: {} ) => {
    try {
      Tools.callApi({
        method: 'POST',
        url:'/api/msgcenter/homepage/getMsgList',
        params,
      }).then(action((res) => {
        const data = res.data.reverse()
        const newState = {
          loading: false,
          current: params.current || 1,
          data: [...(params.current === 1 ? [] : this.list.data), ...data],
          hasMore: data.length >= 10,
          rowKey: 'messageid',
          isTable: false,
          template: {default: []},
          msgid: res.msgid,
          mintime: res.mintime,
        };
        this.list.setState(newState);
        this.list.updateDataSource();
      }));
    } catch (e) {
      Toast.info(e, 1);
    }
  }

}